package cn.tedu.notice.controller;

import cn.tedu.notice.mapper.NoticeMapper;
import cn.tedu.notice.pojo.dto.NoticeQueryParam;
import cn.tedu.notice.pojo.entity.Notice;
import cn.tedu.notice.base.response.JsonResult;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Validated
@RestController
public class NoticeController {

    /**
     * 依赖注入
     */
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/notices")
    public JsonResult insertNotice(@RequestBody @Validated Notice notice){
        notice.setUserId(1L);//将来在这里获取登录用户ID
        noticeMapper.insert(notice);
        return new JsonResult(200,"insert ok");
    }
    @PutMapping("/notices")
    public JsonResult updateNotice(@RequestBody  Notice notice){
        noticeMapper.update(notice);
        return new JsonResult(200,"update ok");
    }

    @DeleteMapping("/notices")
    public JsonResult deleteNotices(@RequestBody Long[] ids){
        System.out.println(Arrays.toString(ids));
        noticeMapper.deleteById(ids);
        return new JsonResult(200,"delete ok");
    }

    @GetMapping("/notices")
    public JsonResult listNotice(@RequestParam(value = "title",required = false) String title,
                                 @RequestParam(value ="type",required = false) Integer type){
        List<Notice> list = noticeMapper.list(title, type);
        return new JsonResult(list);
    }

    /**
     * 基于ID查询公告信息
     * 接口设计：
     * url： /notices/{id}
     * method: GET
     * param: id
     * return: {id:1,title:"",....}
     * @param id
     * @param id
     * @return
     */
    @GetMapping("/notices/{id}")
    public JsonResult selectNoticeById(@PathVariable("id")
                                        @Range(min = 1,max = Long.MAX_VALUE,
                                                message = "id值不合法") Long id){
        Notice notice = noticeMapper.selectById(id);
        return new JsonResult(notice);
    }
}
